export function onMessage (name, callback, once = true) {
  function handleMessage (m) {
    if (m && m.data) {
      var _temp = m.data.replace('undefined', '""')
      var data = eval('(' + _temp + ')')
      if (data.type === name || data.name === name || data.data === name) {
        callback(data)
        if (once) {
          window.removeEventListener('message', handleMessage, false)
        }
      }
    }
  }
  window.addEventListener('message', handleMessage, false)
}
// window.addEventListener会导致绑定的事件出现多次方法调用，故使用window.onmessage
// 目前用于电子签章
export function onMessage2 (name, callback) {
  window.onmessage = function ($event) {
    var _temp
    var data
    console.log($event.data)
    if ($event.data === name) {
      callback(data)
    } else {
      if (typeof $event.data === 'string') {
        _temp = $event.data.replace('undefined', '""')
        data = eval('(' + _temp + ')')
      } else {
        data = $event.data
      }
      if (data === name || data.type === name || data.name === name) {
        callback(data)
        // window.removeEventListener('message', handleMessage, false);
      }
    }
  }
}
